package com.example.zhuyu.basemvp.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.zhuyu.basemvp.di.component.AppComponent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author zhuyu
 * @date 2019/2/17
 * 抽象类可以不实现或者实现一部分接口中的方法，但是普通的类或者抽象类的子类必须实现接口的所有方法
 */
public abstract class BaseActivity<P extends IPresenter> extends RxAppCompatActivity implements IActivity,App{

    @Inject
    protected P mPresenter;

    private Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {

            int layout = initLayout(savedInstanceState);
            if(layout!=0){
                setContentView(layout);
                mUnbinder = ButterKnife.bind(this);
                //禁止横屏(设置为竖屏显示)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        setDaggerComponent(getAppComponent());
        init(savedInstanceState);
        onRequest();

    }

    @Override
    public AppComponent getAppComponent() {
        return BaseApplication.getInstance().getAppComponent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        if (mPresenter != null) {
            mPresenter.onDestroy();
            mPresenter = null;
        }
    }

}
